package com.gyh.thymeleaf.service.impl;

import com.gyh.thymeleaf.entity.Building;
import com.gyh.thymeleaf.entity.BuildingExample;
import com.gyh.thymeleaf.mapper.BuildingMapper;
import com.gyh.thymeleaf.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gyhstart
 * @create 2020/6/26 - 18:45
 **/
@Service
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    private BuildingMapper buildingMapper;

    /**
     * 添加数据
     *
     * @param building
     * @return
     */
    @Override
    public int save(Building building) {
        Building builds = new Building();
        builds.setName(building.getName());
        builds.setType(building.getType());
        int result = buildingMapper.insertSelective(builds);
        return result;
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @Override
    public List<Building> query() {
        BuildingExample example = new BuildingExample();
        example.setOrderByClause("id desc");
        return buildingMapper.selectByExample(example);
    }

    /**
     * 查询数据id
     */
    @Override
    public Building queryById(int id) {
        return buildingMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改数据
     *
     * @param building
     * @return
     */
    @Override
    public int modify(int id, Building building) {
        Building buildings = new Building();
        buildings.setId(id);
        buildings.setName(building.getName());
        buildings.setType(building.getType());
        return buildingMapper.updateByPrimaryKeySelective(buildings);
    }

    /**
     * 删除数据id
     *
     * @param id
     * @return
     */
    @Override
    public int removeById(int id) {
        return buildingMapper.deleteByPrimaryKey(id);
    }

    /**
     * 条件查询
     * @param name
     * @return
     */
    @Override
    public List<Building> conditionBuilding(String name) {
        BuildingExample example = new BuildingExample();
        example.setOrderByClause("id desc");
        if (!"notFull".equals(name)){
            example.createCriteria()
                    .andNameLike("%"+name+"%");
            return buildingMapper.selectByExample(example);
        }else {
            return buildingMapper.selectByExample(example);
        }
    }

}
